In [1]:
import sys
import os
sys.path.append('../src/')
sys.path.append('/home/ipl/installs/caffe-rc/python/')
import matplotlib
%matplotlib inline
from matplotlib import pylab as plt
import numpy as np
import scipy.misc
import scipy.stats
import scipy.io
import caffe
import cv2
import re

import sklearn.svm
import sklearn.metrics
import utils
from datetime import datetime as dt

from dataset import CUB_200_2011
from storage import datastore
from deep_extractor import CNN_Features_CAFFE_REFERENCE, Berkeley_Extractor
from datetime import datetime as dt
import settings
from parts import *
from cub_utils import *
import skimage

In [2]:
cub = CUB_200_2011(settings.CUB_ROOT)
all_image_infos = cub.get_all_image_infos()
IDtrain, IDtest = cub.get_train_test_id()
bbox = cub.get_bbox()
cub_parts = cub.get_parts()

In [3]:
bah = BerkeleyAnnotationsHelper(settings.BERKELEY_ANNOTATION_BASE_PATH, IDtrain, IDtest)

testing oracle performance without fine-tuning


In [43]:
fe_r = CNN_Features_CAFFE_REFERENCE(datastore(settings.storage('ccr')))
fe_c = CNN_Features_CAFFE_REFERENCE(datastore(settings.storage('ccc')))
fe_h = CNN_Features_CAFFE_REFERENCE(datastore(settings.storage('ccphead')))
fe_b = CNN_Features_CAFFE_REFERENCE(datastore(settings.storage('ccpbody')))

Xtrain_r, ytrain_r, Xtest_r, ytest_r = cub.get_train_test(fe_r.extract_one)
Xtrain_c, ytrain_c, Xtest_c, ytest_c = cub.get_train_test(fe_c.extract_one)
Xtrain_h, ytrain_h, Xtest_h, ytest_h = cub.get_train_test(fe_h.extract_one)
Xtrain_b, ytrain_b, Xtest_b, ytest_b = cub.get_train_test(fe_b.extract_one)

In [31]:
ytrain = ytrain_r
ytest = ytest_r

Xtrain = np.concatenate((Xtrain_r, Xtrain_c, Xtrain_h, Xtrain_b), axis=1)
Xtest = np.concatenate((Xtest_r, Xtest_c, Xtest_h, Xtest_b), axis=1)

In [34]:
model = sklearn.svm.LinearSVC(C=0.0001)
model.fit(Xtrain, ytrain)
preds = model.predict(Xtest)

print utils.mean_accuracy(ytest, preds)


0.733028

Investigating Berkeley Annotations


In [4]:
img_id = 654

img_info = all_image_infos[img_id]
img = caffe.io.load_image(img_info)
img_parts = cub_parts.for_image(img_id)

fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(111)

berkeley_head_info = bah.get_berkeley_annotation(img_id, 'head')
berkeley_body_info = bah.get_berkeley_annotation(img_id, 'body')
berkeley_bbox_info = bah.get_berkeley_annotation(img_id, 'bbox')

img_part = utils.get_rect(img, berkeley_body_info)

# img_parts.norm_for_bbox(bbox[img_id-1][0], bbox[img_id-1][1]).draw_part(ax, 'yellow')
ax.imshow(img_part)


Out[4]:
<matplotlib.image.AxesImage at 0x64a6bd0>

Berkeley Oracle Performance with Berkeley Models


In [16]:
fe_r = CNN_Features_CAFFE_REFERENCE(datastore(settings.storage('ccrft')))
fe_c = Berkeley_Extractor(datastore(settings.storage('bmbc')))
fe_h = Berkeley_Extractor(datastore(settings.storage('bmbh')))
fe_b = Berkeley_Extractor(datastore(settings.storage('bmbb')))

Xtrain_r, ytrain_r, Xtest_r, ytest_r = cub.get_train_test(fe_r.extract_one)
Xtrain_c, ytrain_c, Xtest_c, ytest_c = cub.get_train_test(fe_c.extract_one)
Xtrain_h, ytrain_h, Xtest_h, ytest_h = cub.get_train_test(fe_h.extract_one)
Xtrain_b, ytrain_b, Xtest_b, ytest_b = cub.get_train_test(fe_b.extract_one)

In [39]:
ytrain = ytrain_r
ytest = ytest_r

Xtrain = np.concatenate((Xtrain_r, Xtrain_c, Xtrain_h, Xtrain_b), axis=1)
Xtest = np.concatenate((Xtest_r, Xtest_c, Xtest_h, Xtest_b), axis=1)

In [43]:
model = sklearn.svm.LinearSVC(C=10)
model.fit(Xtrain, ytrain)
preds = model.predict(Xtest)

print utils.mean_accuracy(ytest, preds)


0.749521

In [ ]: